<?php

namespace Admin\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Select;
use Zend\Stdlib\DateTime;
use Zend\Session\Container;


class SliderTable extends AbstractTableGateway {

    protected $table = 'slider';

    public function __construct(Adapter $adapter) {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Slider());

        $this->initialize();
    }

    public function fetchAll(Select $select = null) {
        if (null === $select)
            $select = new Select();
        $select->from($this->table);
        $resultSet = $this->selectWith($select);
        $resultSet->buffer();
        return $resultSet;
    }

    public function getSlider($id) {
        $id = (int) $id;
        $rowset = $this->select(array('id' => $id));
        $row = $rowset->current(); 
        return $row;
    }
	public function saveSlider(Slider $slider) {
		
        $id = (int) $slider->id;
        if ($id == 0) {
			
            	$data = array(
					
					'hinhanh' => $slider->hinhanh,
					
					'trangthai' => $slider->trangthai,
					
				);	
                $this->insert($data);
            
        } else {
			if($slider->hinhanhmoi)
			{
				$data = array(
					
					'hinhanh' => $slider->hinhanhmoi,
					
					'trangthai' => $slider->trangthai,
					
				);
			}
			else{
			
				$data = array(
					
					'trangthai' => $slider->trangthai,
					
				);
			}
            if ($this->getSlider($id)) {
				
					$this->update($data, array('id' => $id));
				
            } else {
                throw new \Exception('Không tồn tại');
            }
        }
    }
	public function capnhat($id,$trangthai,$hinhanhmoi)
	{
		if($hinhanhmoi!="")
		{
			$data = array(
				'hinhanh' => $hinhanhmoi,
				'trangthai' => $trangthai
				
			);
		}
		else
		{
		
			$data = array(
				
				'trangthai' => $trangthai
				
			);
		}
            if ($this->getSlider($id)) {
				
					$this->update($data, array('id' => $id));
				
            } else {
                throw new \Exception('Không tồn tại');
            }
		
	}

    public function deleteSlider($id) {
        $this->delete(array('id' => $id));
    }
	public function xoaSlider($id) {
		
        $this->delete('id IN (' . $id . ')');
    }
	public function phathanhSlider($id) {
		
        $this->update(array('trangthai'=>"Hiển thị"), 'id IN (' . $id . ')');
    }
	public function anSlider($id) {
		
        $this->update(array('trangthai'=>"Ẩn"), 'id IN (' . $id . ')');
    }
	//kiem tra ton tai
	public function check_exists($hinhanh,$id)
	{
		//$rowset = $this->select(array('title' => $title));
        //$row = $rowset->current();
		$resultSet = $this->select(array('hinhanh = "'.$hinhanh.'"', 'id != "'.$id.'"'));
    	$row = $resultSet->current();
        return $row;
		
	}
	


}
